declare let IosSelect: any
declare let window: any

/**
 * 年份初始化
 * @param nowYear 当前年份
 */
function formatYear(nowYear: number) {
  const arr = []
  for (let i = nowYear - 10; i <= nowYear + 10; i++) {
    arr.push({
      id: i + '',
      value: i
    })
  }
  return arr
}

/**
 * 月份初始化
 */
function formatMonth() {
  const arr = []
  for (let i = 1; i <= 12; i++) {
    arr.push({
      id: i + '',
      value: i
    })
  }
  return arr
}

/**
 * 日期初始化
 * @param count 当前日期
 */
function formatDate(count: number) {
  const arr = []
  for (let i = 1; i <= count; i++) {
    arr.push({
      id: i + '',
      value: i
    })
  }
  return arr
}

/**
 * 获取月份数据
 * @param year 年份
 * @param callback 回调函数
 */
function getMonthData(year: number, callback: any) {
  callback(formatMonth())
}

/**
 * 获取日期
 * @param year 年份
 * @param month 月份
 * @param callback 回调函数
 */
function getDateData(year: number, month: any, callback: any) {
  if (/^(1|3|5|7|8|10|12)$/.test(month)) {
    callback(formatDate(31))
  } else if (/^(4|6|9|11)$/.test(month)) {
    callback(formatDate(30))
  } else if (/^2$/.test(month)) {
    if (year % 4 === 0 && year % 100 !== 0 || year % 400 === 0) {
      callback(formatDate(29))
    } else {
      callback(formatDate(28))
    }
  } else {
    throw new Error('month is illegal')
  }
}

/**
 * 获取小时数据
 * @param one null
 * @param two null
 * @param three null
 * @param callback 回调函数
 */
function getHourData(one: any, two: any, three: any, callback: any) {
  const hours = []
  for (let i = 0, len = 24; i < len; i++) {
    hours.push({
      id: i,
      value: i
    })
  }
  callback(hours)
}

/**
 * 获取分钟数
 * @param one null
 * @param two null
 * @param three null
 * @param four null
 * @param callback 回调函数
 */
function minuteData(one: any, two: any, three: any, four: any, callback: any) {
  const minutes = []
  for (let i = 0, len = 60; i < len; i++) {
    minutes.push({
      id: i,
      value: i
    })
  }
  callback(minutes)
}

export default {
  /**params
   * data:传递的数组数据（必传）
   * ID：默认选择项ID（必传）
   * calllback:回调函数（必传）
   * title：标题（选传）
   */
  showIosSelect: (data: Array<{
    value: string,
    id: string | number
  }>, id: string | number, callback: any, title?: string) => {
    window.iosSelect = new IosSelect(1,
      [data],
      {
        title: title || '',
        container: '.container',
        itemHeight: 35,
        itemShowCount: 5,
        oneLevelId: id,
        callback: (selectOneObj: { value: string, id: string }) => {
          if (!selectOneObj) {
            callback()
            return
          }
          callback(selectOneObj)
        }
      })
  },
  /**params
   * data:传递的数组数据（必传）
   * ID：默认选择项ID（必传）
   * calllback:回调函数（必传）
   * title：标题（选传）
   */
  showMuelIosSelect: (num: number, data: Array<{
    value: string,
    id: string
  }>, oneLevelId: string, twoLevelId: string, callback: any, title?: string) => {
    window.iosSelect = new IosSelect(num,
      data,
      {
        title: title || '',
        container: '.container',
        itemHeight: 35,
        itemShowCount: 5,
        oneLevelId,
        twoLevelId,
        callback: (selectOneObj: { value: string, id: string }) => {
          if (!selectOneObj) {
            callback()
            return
          }
          callback(selectOneObj)
        }
      })
    // this.events.publish('getiosSelect', window.iosSelect);
  },
  /**
   * 显示日期时间
   * @param time 时间
   * @param callback 回调函数
   * @param title 标题
   */
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  showDateTimeSelect: (time: string, callback: any, title?: string) => {
    // 初始化时间
    let now = new Date()
    if (time) {
      now = new Date(time)
    }
    const nowYear = now.getFullYear()
    const nowMonth = now.getMonth() + 1
    const nowDate = now.getDate()
    const nowHour = now.getHours()
    const nowMin = now.getMinutes()
    // tslint:disable-next-line: only-arrow-functions
    const yearData = function (
      // tslint:disable-next-line: no-shadowed-variable
      callback: any) {
      callback(formatYear(nowYear))
    }
    let monthData = null
    let dateData: any
    let hourData = null
    let minData = null
    getMonthData(nowYear, (res: any) => {
      monthData = res
    })
    getDateData(nowYear, nowMonth, (res: any) => {
      dateData = res
    })
    getHourData(nowYear, nowMonth, nowDate, (res: any) => {
      hourData = res
    })
    minuteData(nowYear, nowMonth, nowDate, nowHour, (res: any) => {
      minData = res
    })
    new IosSelect(5,
      [yearData, monthData, dateData, hourData, minData],
      {
        title: '',
        itemHeight: 35,
        // relation: [1, 1, 0, 0],
        itemShowCount: 7,
        oneLevelId: nowYear,
        twoLevelId: nowMonth,
        threeLevelId: nowDate,
        fourLevelId: nowHour,
        fiveLevelId: nowMin,
        // tslint:disable-next-line: no-shadowed-variable
        getTwoData(data: any, callback: any) {
          dateData(nowYear, data, (res: any) => {
            dateData = res
            callback(dateData)
          })
        },
        callback(selectOneObj: { value: string, id: any }, selectTwoObj: { value: string, id: any }, selectThreeObj: {
          value: string,
          id: any
        }, selectFourObj: { value: string, id: any }, selectFiveObj: { value: string, id: any }) {
          if (!selectOneObj && !selectTwoObj && !selectThreeObj && !selectFourObj && !selectFiveObj) {
            callback()
            return
          }
          // tslint:disable-next-line: radix
          callback(new Date(selectOneObj.id, (parseInt(selectTwoObj.id) - 1), selectThreeObj.id, selectFourObj.id, selectFiveObj.id))
        }
      })
  },
  /**
   * 显示月份选择
   * @param time 时间
   * @param callback 回调函数
   * @param title 标题
   */
  showMonthSelect: (time: string, callback: any, title?: string, isReset?: boolean) => {
    // 初始化时间
    let now
    if (time) {
      now = new Date(time)
    } else {
      now = new Date()
    }
    const nowYear = now.getFullYear()
    const nowMonth = now.getMonth() + 1
    // const nowDate = now.getDate();
    // tslint:disable-next-line: no-shadowed-variable
    const yearData = (callback: any) => {
      callback(formatYear(nowYear))
    }
    let monthData = null
    // const dateData = null;
    getMonthData(nowYear, (res: unknown) => {
      monthData = res
    })
    window.iosSelect = new IosSelect(2,
      [yearData, monthData],
      {
        title: title || '',
        itemHeight: 35,
        // relation: [1, 1, 0, 0],
        itemShowCount: 7,
        oneLevelId: nowYear,
        twoLevelId: nowMonth,
        // threeLevelId: nowDate,
        callback(selectOneObj: { value: string, id: any }, selectTwoObj: { value: string, id: any }) {
          if (!selectOneObj && !selectTwoObj) {
            callback()
            return
          }
          // tslint:disable-next-line: radix
          callback(new Date(selectOneObj.id, (parseInt(selectTwoObj.id) - 1)))
        }
      })
  },
  /**
   * 显示年份选择
   * @param time 时间
   * @param callback 回调函数
   * @param title 标题
   */
  showYear: (time: any, callback: any, title?: string, isReset?: boolean) => {
    // 初始化时间
    let now
    if (time) {
      now = new Date(time)
    } else {
      now = new Date()
    }
    const nowYear = now.getFullYear()
    const nowMonth = now.getMonth() + 1
    const nowDate = now.getDate()
    // tslint:disable-next-line: no-shadowed-variable
    const yearData = (callback: any) => {
      callback(formatYear(nowYear))
    }
    window.iosSelect = new IosSelect(1,
      [yearData],
      {
        title: title || '',
        itemHeight: 35,
        // relation: [1, 1, 0, 0],
        itemShowCount: 7,
        oneLevelId: nowYear,
        twoLevelId: nowMonth,
        threeLevelId: nowDate,
        // eslint-disable-next-line @typescript-eslint/no-unused-vars
        callback(selectOneObj: { value: string, id: any }, selectTwoObj: { value: string, id: any }, selectThreeObj: {
          value: string,
          id: any
        }) {
          if (!selectOneObj && !selectTwoObj && !selectThreeObj) {
            callback()
            return
          }
          callback(new Date(selectOneObj.id))
        }
      })
  },
  /**
   * 显示日期选择
   * @param time 时间
   * @param callback 回调函数
   * @param title 标题
   * @param isReset 是否显示重置按钮
   */
  showDateSelect: (time: any, callback: any, title?: string, isReset?: boolean) => {
    // 初始化时间
    let now
    if (time) {
      now = new Date(time)
    } else {
      now = new Date()
    }
    const nowYear = now.getFullYear()
    const nowMonth = now.getMonth() + 1
    const nowDate = now.getDate()
    // tslint:disable-next-line: no-shadowed-variable
    const yearData = (callback: any) => {
      callback(formatYear(nowYear))
    }
    let monthData: any = []
    let dateData: any = []
    getMonthData(nowYear, (res: any) => {
      monthData = res
    })
    getDateData(nowYear, nowMonth, (res: any) => {
      dateData = res
    })
    window.iosSelect = new IosSelect(3,
      [yearData, monthData, dateData],
      {
        title: title || '',
        isReset: isReset || false,
        itemHeight: 35,
        // relation: [1, 1, 0, 0],
        itemShowCount: 7,
        oneLevelId: nowYear,
        twoLevelId: nowMonth,
        threeLevelId: nowDate,
        getOneData(data: { value: string, id: any }, callback: any) {
          console.log(window.iosSelect);
          getDateData(window.iosSelect.selectOneObj.value, window.iosSelect.selectTwoObj.value, (res: any) => {
            dateData = res
            console.log('dateData', dateData)
            callback(monthData, dateData)
          })
        },
        // tslint:disable-next-line: no-shadowed-variable
        getTwoData(data: { value: string, id: any }, callback: any) {
          console.log(window.iosSelect);
          getDateData(window.iosSelect.selectOneObj.value, data, (res: any) => {
            dateData = res
            callback([], dateData)
          })
        },
        callback(selectOneObj: { value: string, id: any }, selectTwoObj: { value: string, id: any }, selectThreeObj: {
          value: string,
          id: any
        }) {
          if (!selectOneObj && !selectTwoObj && !selectThreeObj) {
            callback()
            return
          }
          // tslint:disable-next-line: radix
          callback(new Date(selectOneObj.id, (parseInt(selectTwoObj.id) - 1), selectThreeObj.id))
        }
      })
  },

  /**
   * 日期对象转为日期字符串
   * @param date 需要格式化的日期对象
   * @param sFormat 输出格式,默认为yyyy-MM-dd                        年：y，月：M，日：d，时：h，分：m，秒：s
   * @example  dateFormat(new Date())                               "2017-02-28"
   * @example  dateFormat(new Date(),'yyyy-MM-dd')                  "2017-02-28"
   * @example  dateFormat(new Date(),'yyyy-MM-dd HH:mm:ss')         "2017-02-28 13:24:00"   ps:HH:24小时制
   * @example  dateFormat(new Date(),'yyyy-MM-dd hh:mm:ss')         "2017-02-28 01:24:00"   ps:hh:12小时制
   * @example  dateFormat(new Date(),'hh:mm')                       "09:24"
   * @example  dateFormat(new Date(),'yyyy-MM-ddTHH:mm:ss+08:00')   "2017-02-28T13:24:00+08:00"
   * @example  dateFormat(new Date('2017-02-28 13:24:00'),'yyyy-MM-ddTHH:mm:ss+08:00')   "2017-02-28T13:24:00+08:00"
   */
  dateFormat: (date: Date, sFormat: string) => {
    date = new Date(date)
    const time = {
      Year: 0,
      TYear: '0',
      Month: 0,
      TMonth: '0',
      Day: 0,
      TDay: '0',
      Hour: 0,
      THour: '0',
      hour: 0,
      Thour: '0',
      Minute: 0,
      TMinute: '0',
      Second: 0,
      TSecond: '0',
      Millisecond: 0
    }
    time.Year = date.getFullYear()
    time.TYear = String(time.Year).substr(2)
    time.Month = date.getMonth() + 1
    time.TMonth = time.Month < 10 ? '0' + time.Month : String(time.Month)
    time.Day = date.getDate()
    time.TDay = time.Day < 10 ? '0' + time.Day : String(time.Day)
    time.Hour = date.getHours()
    time.THour = time.Hour < 10 ? '0' + time.Hour : String(time.Hour)
    time.hour = time.Hour < 13 ? time.Hour : time.Hour - 12
    time.Thour = time.hour < 10 ? '0' + time.hour : String(time.hour)
    time.Minute = date.getMinutes()
    time.TMinute = time.Minute < 10 ? '0' + time.Minute : String(time.Minute)
    time.Second = date.getSeconds()
    time.TSecond = time.Second < 10 ? '0' + time.Second : String(time.Second)
    time.Millisecond = date.getMilliseconds()

    return sFormat.replace(/yyyy/ig, String(time.Year))
      .replace(/yyy/ig, String(time.Year))
      .replace(/yy/ig, time.TYear)
      .replace(/y/ig, time.TYear)
      .replace(/MM/g, time.TMonth)
      .replace(/M/g, String(time.Month))
      .replace(/dd/ig, time.TDay)
      .replace(/d/ig, String(time.Day))
      .replace(/HH/g, time.THour)
      .replace(/H/g, String(time.Hour))
      .replace(/hh/g, time.Thour)
      .replace(/h/g, String(time.hour))
      .replace(/mm/g, time.TMinute)
      .replace(/m/g, String(time.Minute))
      .replace(/ss/ig, time.TSecond)
      .replace(/s/ig, String(time.Second))
      .replace(/fff/ig, String(time.Millisecond))
  }
}
